package wnhz.com.business.groupshop.ui;

import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.R;
import wnhz.com.business.groupshop.adapter.MainGoodsAdapter;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.base.BaseRecyclerAdapter;
import wnhz.com.business.groupshop.bean.GoodsBean;
import wnhz.com.business.constants.EXTRA;
import wnhz.com.business.groupshop.bean.HomeIndexBean;
import wnhz.com.business.uitls.decoration.DividerGridItemDecoration;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.view.TranslucentActionBar;

/**
 * 商品分类
 */
public class CommodityClassifyActivity extends BaseActivity implements ActionBarClickListener {

    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.sliding_tabs)
    TabLayout slidingTabs;
    @BindView(R.id.recycler_classify)
    RecyclerView recyclerClassify;
    private int type;

    public String tabTitles[] = new String[]{"首页", "奶粉", "孕衣", "玩具", "美食", "纸尿裤"};
    public String shuiguotabTitles[] = new String[]{"首页", "热带", "应季", "瓜", "美食", "榴莲"};
    public String meizhuangtabTitles[] = new String[]{"首页", "口红", "眼影", "眉笔", "香水", "粉底"};
    private List<HomeIndexBean.InfoBean.GoodsBean> goodsBeenList = new ArrayList<>();

    private MainGoodsAdapter mainGoodsAdapter;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commodity_classify);
        ButterKnife.bind(this);
        initview();
        initTabLayout();
        actionbar.setData(title, R.drawable.ic_lefft_white, null, 0, null, 0,this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }


    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }


    private void initview()
    {
        type = getIntent().getIntExtra("type",0);
        mainGoodsAdapter = new MainGoodsAdapter(getApplicationContext(), goodsBeenList);
        mainGoodsAdapter.loadDataFirstTime();

        GridLayoutManager gridManager = new GridLayoutManager(this, 2,
                LinearLayoutManager.VERTICAL, false);  //自定义解决RecyclerView在ScrollView中滑动卡顿现象
//        gridManager.setScrollEnabled(false);

        recyclerClassify.setLayoutManager(gridManager);
        recyclerClassify.addItemDecoration(new DividerGridItemDecoration(20));//分割线
        recyclerClassify.setAdapter(mainGoodsAdapter);

        gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int spansize = 1;
                switch (mainGoodsAdapter.getItemViewType(position)) {
                    case EXTRA.TYPE_FOOTER:
                        spansize = 2;
                        break;
                    case EXTRA.TYPE_HEADER:
                        spansize = 2;
                        break;
                }
                return spansize;
            }
        });


        mainGoodsAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object data) {
                GoodsBean goodsBean = (GoodsBean)data;


            }
        });
    }

    /**
     * 加载数据
     */
    private void initTabLayout() {

        switch (type)
        {
            case 0:
                for (int i = 0; i < shuiguotabTitles.length; i++) {
                    slidingTabs.addTab(slidingTabs.newTab().setText(shuiguotabTitles[i]));
                }
                title = "水果";
                break;
            case 1:
                for (int i = 0; i < meizhuangtabTitles.length; i++) {
                    slidingTabs.addTab(slidingTabs.newTab().setText(meizhuangtabTitles[i]));
                }
                title = "美妆";
                break;
            case 2:
                title = "美容";
                break;
            case 3:
                title = "护肤";
                break;
            case 4:
                title = "彩妆";
                break;
            case 5:
                title = "香水";
                break;
            case 6:
                title = "男士护理";
                break;
            case 7:
                title = "电器";
                break;
            case 8:
                for (int i = 0; i < tabTitles.length; i++) {
                    slidingTabs.addTab(slidingTabs.newTab().setText(tabTitles[i]));
                }
                title = "家纺";
                break;
            case 9:
                title = "更多";
                break;
        }


        slidingTabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
//                initView();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }



}
